package com.hdd.biz;

import com.hdd.biz.app.dto.AppResponse;
import com.hdd.biz.app.dto.MyWorksRequest;
import com.hdd.biz.app.entity.PaintingWorksModel;
import com.hdd.biz.app.service.UserCenterService;
import com.hdd.biz.finance.dto.PaymetListRequest;
import com.hdd.biz.member.service.MemberService;
import com.hdd.dto.SuperResponse;
import com.hdd.utils.JsonUtil;
import com.hdd.utils.TimeUtils;
import com.hdd.utils.wx.RedisUtil;
import com.hdd.utils.wx.WxImageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/test")
@Api(value = "图片上传测试",tags = {"图片上传测试"})
@Slf4j
public class TestController {

    @Value("${files.path}")
    private String filesPath;
    @Autowired
    private WxImageUtil wxImageUtil;

    @Autowired
    private MemberService memberService;
    @Autowired
    private RedisUtil  redisUtil;
    @Autowired
    private UserCenterService service;

    @GetMapping("/upload")
    @ApiOperation(value = "上传" ,notes = "")
    public SuperResponse record(@Valid @RequestBody PaymetListRequest request ,HttpServletRequest httpServletRequest,@RequestParam(value = "file") MultipartFile file){
        SuperResponse response = new SuperResponse();
        log.info("进入");
        String fileName = file.getOriginalFilename();  // 文件名
        log.info("fileName:"+fileName);
        String url = wxImageUtil.uploadImage(filesPath, httpServletRequest);
        log.info(url);
        return response;
    }


    @GetMapping("/m")
    @ApiOperation(value = "上传" ,notes = "")
    public SuperResponse m(){
        SuperResponse response = new SuperResponse();

        if(redisUtil.hasKey("messageByReward:"+ TimeUtils.getTime2()+":"+1)){
         Integer nums =  Integer.parseInt(redisUtil.get("messageByReward:"+ TimeUtils.getTime2()+":"+1).toString());
         if(nums<3){ //一天最多三次
             System.out.println("发送短信");
             redisUtil.set("messageByReward:"+ TimeUtils.getTime2()+":"+1,nums+1, TimeUnit.DAYS.toDays(60*60*24));
         }

        }else{
            System.out.println("发送短信");
            redisUtil.set("messageByReward:"+ TimeUtils.getTime2()+":"+1,1, TimeUnit.DAYS.toDays(60*60*24));
        }

        return response;
    }


    @GetMapping("/redis_list")
    @ApiOperation(value = "redis_list" ,notes = "")
    public SuperResponse redis_list(){
        SuperResponse response = new SuperResponse();

        MyWorksRequest request = new MyWorksRequest();
        request.setPageNo(1);
        request.setPageSize(1000);
        request.setUserId("8cf0362bfab34a33ad34a0b9f8b31aa5");

        if(redisUtil.hasKey("likeList:"+request.getUserId())){
         List<Object> str =  redisUtil.lGet("likeList:"+request.getUserId(),0,1);
         String str1 = str.get(0).toString();
         log.info(str1);
         System.out.println(str1);
        }else{
            AppResponse res =  service.myLikeList(request);
            List<PaintingWorksModel> list = res.getItems();
            int a=1;
            for(PaintingWorksModel p:list){
                p.setIndex(a);
                a = a+1;
                redisUtil.lSet("likeList:" + request.getUserId(), JsonUtil.toJsonString(p));
            }
        }

        return response;
    }



    @GetMapping("/redis_list2")
    @ApiOperation(value = "redis_list2" ,notes = "")
    public SuperResponse redis_list2(){
        SuperResponse response = new SuperResponse();

        MyWorksRequest request = new MyWorksRequest();
        request.setPageNo(1);
        request.setPageSize(1000);
        request.setUserId("8cf0362bfab34a33ad34a0b9f8b31aa5");


        Long index = 3L;
        if(redisUtil.hasKey("likeList:"+request.getUserId())){
            Long size = redisUtil.lGetListSize("likeList:"+request.getUserId());
            System.out.println("size:"+size);
            String string =  redisUtil.lGetIndex("likeList:"+request.getUserId(),3).toString();
            System.out.println("string:"+string);

            List<Object> list1 = redisUtil.lGet("likeList:"+request.getUserId(),(index-5<=0)?0:(index-5),index-1);
            System.out.println("list1:"+list1.toString());

            List<Object> list2 = redisUtil.lGet("likeList:"+request.getUserId(),index+1,(index+1+5)>size?size:(index+1+5));

            System.out.println("list2:"+list2.toString());

        }

        return response;
    }


    public static void main(String[] args) {
        Document document = null;
        try {
            document  = Jsoup.connect("https://cli.im/Home/Weapp/create").timeout(40000).ignoreContentType(true)
            .data("weapp_id","wx59c864c7d2fc6536")
            .data("weapp_secret","3b6926886a620aed827c70a23b9a8fdd")
            .data("weapp_url","3333").post();
            System.out.println(""+document.text());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
